package store.lunangangster.nodeflow.core.component;

import com.alibaba.ttl.TransmittableThreadLocal;
import store.lunangangster.nodeflow.enums.NodeType;
import store.lunangangster.nodeflow.util.KeyGenerator;

/**
 * <h2>循环退出Node</h2>
 * <p>对于任意一个LoopFlow，都会使用该类的对象作为每次循环执行前的退出执行判断
 *
 * @author cwk
 * @version 1.0
 */
public abstract class ExitComponent extends NodeComponent {

  private static final ThreadLocal<String> EXIT_KEY_TL = new TransmittableThreadLocal<>();

  @Override
  public void process() {
    boolean exitResult = processExit();
    String key = KeyGenerator.generate(NodeType.NODE_EXIT);
    EXIT_KEY_TL.set(key);
    this.getSlot().setData(key, exitResult);
  }

  /**
   * 根据业务选择是否退出循环节点
   * @return  是否退出循环节点
   */
  public abstract boolean processExit();

  @Override
  public String getExitResultKey() {
    return EXIT_KEY_TL.get();
  }

  @Override
  public void removeExitResultKey() {
    EXIT_KEY_TL.remove();
  }
}
